let fs = require('fs');
// 读取文件
module.exports.getBaseData = (path) => {
    return new Promise((resolve, reject) => {
        fs.readFile(path, 'utf-8', (err, res) => {
            if (err) {
                console.log(err);
                reject('read file error');
            }
            var data = JSON.parse(res);
            resolve(data);
        })
    })
}
const options = {
    flag: 'w',
    encoding: 'utf-8',
    mode: '0666'
}
// 写入文件
module.exports.setBaseData = (path, data) => {
    return new Promise((resolve, reject) => {
        fs.writeFile(path, data, 'utf-8', (err, res) => {
            if (err) {
                console.log(err);
                reject('read file error');
            }
            var data = JSON.parse(res);
            resolve(data);
        })
    })
}
// 读取并写入文件
module.exports.save = function (path, sentence, callback) {
    fs.readFile(path, 'utf8', function (err, data) {
        if (err) {
            return callback(err)
        }
        var {
            data
        } = JSON.parse(data)
        console.log(sentence);
        // 遍历拷贝对象
        for (var key in sentence) {
            data[key].push(sentence[key])
        }
        // console.log(data.);
        // // 添加 id ，唯一不重复
        // sentence.id = students[students.length - 1].id + 1

        // // 把用户传递的对象保存到数组中
        // students.push(sentence)

        // 把对象数据转换为字符串
        console.log(data);
        var fileData = JSON.stringify({
            data
        })

        // 把字符串保存到文件中
        fs.writeFile(path, fileData, 'utf8', function (err) {
            if (err) {
                // 错误就是把错误对象传递给它
                return callback(err)
            }
            // 成功就没错，所以错误对象是 null
            callback(null)
        })
    })
}
// 查询文件
module.exports.searchData = (path, str) => {
    return new Promise((resolve, reject) => {
        fs.readFile(path, 'utf-8', (err, res) => {
            if (err) {
                console.log(err);
                reject('read file error');
            }
            var {
                data
            } = JSON.parse(res);
            let reslist = []
            for (var key in data) {
                for (var item of data[key]) {
                    if (item.indexOf(str) > -1) {
                        reslist.push(item)
                    }
                }
            }
            console.log(reslist);
            resolve(reslist);
        })
    })
}

function search(str, container) {
    var newList = [];
    //新的列表
    var startChar = str.charAt(0);
    //开始字符
    var strLen = str.length;
    //查找符串的长度


    for (var i = 0; i < container.length; i++) {
        var obj = container[i];
        var isMatch = false;
        for (var p in obj) {
            if (typeof (obj[p]) == "function") {
                obj[p]();
            } else {
                var curItem = "";
                if (obj[p] != null) {
                    curItem = obj[p];
                }
                for (var j = 0; j < curItem.length; j++) {
                    if (curItem.charAt(j) == startChar) //如果匹配起始字符,开始查找
                    {
                        if (curItem.substring(j).substring(0, strLen) == str) //如果从j开始的字符与str匹配，那ok
                        {
                            isMatch = true;
                            break;
                        }
                    }
                }
            }
        }
        if (isMatch) {
            newList.push(obj);
        }
    }
    return newList;
}
// 读取文件名
module.exports.getFileName = (path, fileClass) => {
    return new Promise((resolve, reject) => {
        fs.readdir(path, (err, files) => {
            if (err) {
                console.log(err);
                reject('read file error');
            }
            var files_arr = [];
            files_arr = files.map((file) => {
                return 'http://localhost:8888/images/' + fileClass + '/' + file;
            })
            resolve(files_arr);
        })
    })
}